<?php
require_once BaseUtil::getModulePath("base")  .  '/services/BaseService.php';
require_once BaseUtil::getModulePath("systemlog")  .  '/models/EmailLogDao.php';
require_once BaseUtil::getModulePath("systemlog")  .  '/constants/LogConstant.php';
require_once BaseUtil::getModulePath("order")  .  '/models/OrderDao.php';
require_once BaseUtil::getModulePath("order")  .  '/models/OrderItemsDao.php';
require_once BaseUtil::getModulePath("customer")  .  '/models/CustomerDao.php';
require_once BaseUtil::getModulePath("component")  .  '/MailUtil.php';


class EmailService extends BaseService {
    private $emailLogDao;
    private $orderDao;
    private $orderItemDao;
    private $customerDao;

    public function  __construct() {
        parent::__construct();
        $this->emailLogDao=new EmailLogDao();
        $this->emailLogDao->setDb($this->db);
        $this->orderDao=new OrderDao();
        $this->orderDao->setDb($this->db);
        $this->orderItemDao=new OrderItemsDao();
        $this->orderItemDao->setDb($this->db);
        $this->customerDao=new CustomerDao();
        $this->customerDao->setDb($this->db);
    }
    
    /**
     * 1.订单提交通知邮件
     * 2.订单确认通知邮件
     * 3.已发货通知邮件
     * 4.校园大使已收货通知邮件
     * 5.订单交易成功通知邮件
     * 6.用户注册通知邮件
     * 7.用户找回密码通知邮件
     * 8.后台管理群发通知邮件
     */
    
    /**
     * 订单交易成功通知邮件
     */
    public function orderSuccessEmailToUser($param){
    	//邮件title
    	$successEmailTitle = $param["successEmailTitle"];
    	$orderId = $param["orderId"];
    	//得到order信息
    	$orderInfo = $this->orderDao->getByCol("id", $orderId);
        $orderNumber = $orderInfo["order_num"];
        $orderTotal = $orderInfo["total"];
        $orderItemInfo = $this->orderItemDao->getItemsById($orderId);
    	//得到用户信息
    	$userId = $orderInfo["user_id"];
    	$userInfo = $this->customerDao->getByCol("id", $userId);
        $status = $userInfo["status"];
        $userName = $userInfo["name"];
        $email = $userInfo["email"];
        $orderHtml =
<<<str_result
<table style="font-family:'宋体'; font-size:12px; color:#666666;">
        <!-- header begin -->
    
        <tr>
            <td background="http://i.akimimi.com/t/default_zh_cn/image/email/email_002.jpg" style="width:650px;height:95px;" colspan="2">
                <br/><br/><br/><br/><br/>
                    <span>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    </span>
                    <span><a style="text-decoration:none;color:#F0FFEA;" href="http://www.akimimi.com">首页</a></span>&nbsp;&nbsp;&nbsp;
                    <span><a style="text-decoration:none;color:#F0FFEA;" href="http://www.akimimi.com/c/index/mobile_and_accessories">手机</a></span>&nbsp;&nbsp;&nbsp;
                    <span><a style="text-decoration:none;color:#F0FFEA;" href="http://www.akimimi.com/c/index/dc_and_accessories">数码相机</a></span>&nbsp;&nbsp;&nbsp;
                    <span><a style="text-decoration:none;color:#F0FFEA;" href="http://www.akimimi.com/c/index/women_costume">时尚女装</a></span>&nbsp;&nbsp;&nbsp;
                    <span><a style="text-decoration:none;color:#F0FFEA;" href="http://www.akimimi.com/c/index/makeup">化妆品</a></span>
            </td>       
        </tr>
        <!-- header end -->
        
        <!-- center end -->
        <tr>
            <td>
                <table>
                    <tr style="color:#000000">
                        <td>
                            <span style="font-weight:bold;">亲爱的：{$userName}</span><br/><br/>
                                                                            您的订单{$orderNumber}已交易成功。感谢您对阿基米米的支持，祝您购物愉快！<br/><br/>
                            <span style="color:#66B331;">您还可以对该订单的商品进行评价，并且可以获得10个积分。现在就去评价吧！</span><br/><br/>
str_result;
        
        foreach ($orderItemInfo as $item) {
        	$orderHtml .= "<a target='_blank' href='http://www.akimimi.com/p/{$item[url_title]}'> http://www.akimimi.com/p/{$item[url_title]}</a><br/>";
        }
        
        $orderHtml .=
<<<str_result
                            <br/><span style="color:#a6a6a6;">如果您不能点击此链接，请将以上链接复制到浏览器地址栏中访问。</span>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <!--<table width="440" border="0" align="left" cellpadding="0" cellspacing="1" >
                                <tr>
                                    <td width="250" height="" align="center"></td>
                                    <td width="89" align="center"></td>
                                    <td width="73" align="center"></td>
                                </tr>-->
str_result;
        
         $orderHtml .=
<<<str_result
                            <!--</table>-->
                        </td>
                    </tr>
                    <tr style="color:#000000;line-height:22px;">
                        <td>
                            <span style="font-weight:bold; color:#FF0000;">温馨提示：</span>
                            对商品进行评论，可以获得积分（<a href="http://www.akimimi.com/u/help?type=mark">点击查看积分规则</a>）；<br/>
                            积分用途： a 可以兑换指定商品; <br/>
           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b 可提高用户等级。<br/>
                            如有任何疑问，欢迎致电：400-66-19686，或联系在线客服。<br/><br/>
                            欢迎您再次到阿基米米购物，祝您购物愉快！
                            <br/><br/>
                        </td>
                    </tr>
                </table>
            </td>
            <td style="width:175px;" valign="top">
                <img src="http://i.akimimi.com/t/default_zh_cn/image/email/email_006.jpg"  alt=""/>
            </td>
        </tr>
        <!-- center end -->
        
        <!-- center begin -->
        <tr>
            <td colspan="2" valign="top">
                <img src="http://i.akimimi.com/t/default_zh_cn/image/email/email_001.jpg" alt="" />
            </td>
        </tr>
        <!-- center end -->
    
</table>  
      
str_result;

         if(!empty($status)){
         	//发送email
	         MailUtil::sendMail($email, $successEmailTitle, $orderHtml);
	        //保存email日志
	        $email_type = 5;
	        $send_to = "$userName<$email>";
	        $send_cc = "";
	        $description = "订单<{$orderNumber}>交易成功通知邮件";
	        $arr_param = array(
	            'email_type' => $email_type,
	            'send_to' => $send_to,
	            'send_cc' => $send_cc,
	            'description' => $description            
	        );
	        $this->emailLogDao->insertData($arr_param);
         }
        
    }
    
    /*
     * 3.已发货通知邮件
     */
    public function sendedGoogsEmailToUser($param){
    	//邮件title
        $shippedInformEmail = $param["shippedInformEmail"];
        //订单信息
        $orderId = $param["orderId"];
        $orderInfo = $this->orderDao->getByCol("id", $orderId);
        $orderNumber = $orderInfo["order_num"];
        $orderTotal = $orderInfo["total"];
        $orderItemInfo = $this->orderItemDao->getItemsById($orderId);
        //得到用户信息
        $userId = $orderInfo["user_id"];
        $userInfo = $this->customerDao->getByCol("id", $userId);
        $userName = $userInfo["name"];
        $email = $userInfo["email"];
        $userCertf = $userInfo["certf"];
        $status = $userInfo["status"];
        //得到大使信息
        $agentId = $orderInfo["agent_id"];
        if(!empty($agentId)){//当前学校有大使
            $agentUserInfo = $this->customerDao->getByCol("id", $agentId);
	        $agentUserName = $agentUserInfo["name"];
	        $agentEmail = $agentUserInfo["email"];
        }
    	
    	//邮件主体
    	$orderHtml1 =
<<<str_result
<table style="font-family:'宋体'; font-size:12px; color:#666666;">
        <!-- header begin -->
    
        <tr>
            <td background="http://i.akimimi.com/t/default_zh_cn/image/email/email_002.jpg" style="width:650px;height:95px;" colspan="2">
                <br/><br/><br/><br/><br/>
                    <span>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    </span>
                    <span><a style="text-decoration:none;color:#F0FFEA;" href="http://www.akimimi.com">首页</a></span>&nbsp;&nbsp;&nbsp;
                    <span><a style="text-decoration:none;color:#F0FFEA;" href="http://www.akimimi.com/c/index/mobile_and_accessories">手机</a></span>&nbsp;&nbsp;&nbsp;
                    <span><a style="text-decoration:none;color:#F0FFEA;" href="http://www.akimimi.com/c/index/dc_and_accessories">数码相机</a></span>&nbsp;&nbsp;&nbsp;
                    <span><a style="text-decoration:none;color:#F0FFEA;" href="http://www.akimimi.com/c/index/women_costume">时尚女装</a></span>&nbsp;&nbsp;&nbsp;
                    <span><a style="text-decoration:none;color:#F0FFEA;" href="http://www.akimimi.com/c/index/makeup">化妆品</a></span>
            </td>       
        </tr>
        <!-- header end -->
        
        <!-- center end -->
        <tr>
            <td>
                <table>
                    <tr style="color:#000000">
                        <td>
str_result;
        $orderHtmlUser =
<<<str_result
                            <span style="font-weight:bold;">亲爱的：{$userName}</span><br/><br/>
                                                                            您的订单{$orderNumber}已经发货，请您耐心等待，校园大使会尽快与您联系。<br/><br/>
str_result;
        $orderHtmlAgent =
<<<str_result
                            <span style="font-weight:bold;">亲爱的：{$agentUserName}</span><br/><br/>
                                                                            订单{$orderNumber}已发货，请您注意查收！<br/><br/>
str_result;
        $orderHtml4 =
<<<str_result
                                                                            订单信息<br/><br/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <table  width="440" border="0" align="left" cellpadding="0" cellspacing="1" bgcolor="#F7FFF4">
                                <tr>
                                    <td width="250" height="25" align="center">商品名称</td>
                                    <td width="89" align="center">数量</td>
                                    <td width="73" align="center">单价</td>
                                </tr>
str_result;
        //显示订单详情
        $orderHtml5 = "";
        foreach ($orderItemInfo as $item) {
            $orderHtml5 .="
                                <tr>
                                    <td height='25' align='center'><a href='http://www.akimimi.com/p/{$item["url_title"]}'>{$item["title"]}</a></td>
                                    <td align='center'>{$item["quantity"]}</td>
                                    <td align='center'>￥{$item["deal_price"]}</td>
                                </tr>
                          ";
        }
        
        $orderHtml6 ="                                
                                <tr>
                                    <td height='25' align='center'>&nbsp;</td>
                                    <td colspan='2' align='center'><h3 style='color:#47A618'>合计：￥{$orderTotal}</h3></td>
                                </tr>
                                ";
        
         $orderHtml7 =
<<<str_result
                            </table>
                        </td>
                    </tr>
                    <tr style="color:#000000;line-height:22px;">
                        <td>
                            <span style="font-weight:bold; color:#FF0000;">温馨提示：</span>
                            您所订购的商品已经发货。阿基米米部分商品由北京库房直接发送，部<br/>
                            分商品由各地分仓发送。为了确保您订购的商品在运输途中不受损害，在发货前，每<br/>
                            一件商品都经过特殊的严密包装，并且托付中国最专业的快递公司负责全程配送。如<br/>
                            果您希望查询商品的运输情况。<br/>
                            可以在以下网站查询到您的物流信息：<br/>
                            顺丰快递：<a target="_blank" href="http://www.sf-express.com">http://www.sf-express.com</a><br/>
	                申通快递：<a target="_blank" href="http://www.sto.cn">http://www.sto.cn</a><br/>
	                中通快递：<a target="_blank" href="http://www.zto.cn">http://www.zto.cn</a><br/>
	                圆通快递：<a target="_blank" href="http://www.yto.ne.t.cn">http://www.yto.ne.t.cn</a><br/>
	                韵达快递：<a target="_blank" href="http://www.yundaex.com">http://www.yundaex.com</a><br/><br/>
                            欢迎您再次到阿基米米购物，祝您购物愉快！
                            <br/>
                        </td>
                    </tr>
                </table>
            </td>
            <td style="width:175px;" valign="top">
                <img src="http://i.akimimi.com/t/default_zh_cn/image/email/email_005.jpg"  alt=""/>
            </td>
        </tr>
        <!-- center end -->
        
        <!-- center begin -->
        <tr>
            <td colspan="2" valign="top">
                <img src="http://i.akimimi.com/t/default_zh_cn/image/email/email_001.jpg" alt="" />
            </td>
        </tr>
        <!-- center end -->
    
</table>  
      
str_result;

	        $orderHtmlToUser = $orderHtml1.$orderHtmlUser.$orderHtml4.$orderHtml5.$orderHtml6.$orderHtml7;
	        $orderHtmlToAgent = $orderHtml1.$orderHtmlAgent.$orderHtml4.$orderHtml5.$orderHtml6.$orderHtml7;
	        //保存email日志
            $email_type = 3;
            $send_to_user = "$userName<$email>";
            $send_to_agent = "$agentUserName<$agentEmail>";
            $send_cc = "";
            $description = "订单<{$orderNumber}>已发货通知邮件";
            $arr_param_user = array(
                'email_type' => $email_type,
                'send_to' => $send_to_user,
                'send_cc' => $send_cc,
                'description' => $description
            );
            
        if(!empty($status)){        
	        //发信
	        MailUtil::sendMail($email, $shippedInformEmail, $orderHtmlToUser);
	        $this->emailLogDao->insertData($arr_param_user);
        }
	        if(!empty($agentId)){//当前学校有大使
		        $arr_param_agent = array(
		            'email_type' => $email_type,
		            'send_to' => $send_to_agent,
		            'send_cc' => $send_cc,
		            'description' => $description
		        );
	            $this->emailLogDao->insertData($arr_param_agent);
	        }
       
        
        if(!empty($agentId)){//当前学校有大使
            MailUtil::sendMail($agentEmail, $shippedInformEmail, $orderHtmlToAgent);
        }
        
        
    }
    
    /**
     * 2.订单确认发邮件给下单用户
     */
    public function orderConfirmationEmailToUser($param){
        //邮件title
        $orderConfirmEmailTitle = $param["orderConfirmEmailTitle"];
        //订单信息
        $orderId = $param["orderId"];
        $orderInfo = $this->orderDao->getByCol("id", $orderId);
        $orderNumber = $orderInfo["order_num"];
        $orderTotal = $orderInfo["total"];
        $orderItemInfo = $this->orderItemDao->getItemsById($orderId);
        //得到用户信息
        $userId = $orderInfo["user_id"];
        $userInfo = $this->customerDao->getByCol("id", $userId);
        $userName = $userInfo["name"];
        $email = $userInfo["email"];
        $status = $userInfo["status"];
        //邮件主体
        $orderHtml =
<<<str_result
<table style="font-family:'宋体'; font-size:12px; color:#666666;">
        <!-- header begin -->
        <tr>
            <td background="http://i.akimimi.com/t/default_zh_cn/image/email/email_002.jpg" style="width:650px;height:95px;" colspan="2">
                <br/><br/><br/><br/><br/>
                    <span>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    </span>
                    <span><a style="text-decoration:none;color:#F0FFEA;" href="http://www.akimimi.com">首页</a></span>&nbsp;&nbsp;&nbsp;
                    <span><a style="text-decoration:none;color:#F0FFEA;" href="http://www.akimimi.com/c/index/mobile_and_accessories">手机</a></span>&nbsp;&nbsp;&nbsp;
                    <span><a style="text-decoration:none;color:#F0FFEA;" href="http://www.akimimi.com/c/index/dc_and_accessories">数码相机</a></span>&nbsp;&nbsp;&nbsp;
                    <span><a style="text-decoration:none;color:#F0FFEA;" href="http://www.akimimi.com/c/index/women_costume">时尚女装</a></span>&nbsp;&nbsp;&nbsp;
                    <span><a style="text-decoration:none;color:#F0FFEA;" href="http://www.akimimi.com/c/index/makeup">化妆品</a></span>
            </td>       
        </tr>
        <!-- header end -->
        
        <!-- center end -->
        <tr>
            <td>
                <table>
                    <tr style="color:#000000">
                        <td>
                            <span style="font-weight:bold;">亲爱的：{$userName}</span><br/><br/>
                                                                            您的订单{$orderNumber}已确认！<br/><br/>
                                                                            订单信息<br/><br/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <table  width="440" border="0" align="left" cellpadding="0" cellspacing="1" bgcolor="#F7FFF4">
                                <tr>
                                    <td width="250" height="25" align="center">商品名称</td>
                                    <td width="89" align="center">数量</td>
                                    <td width="73" align="center">单价</td>
                                </tr>
str_result;
        //显示订单详情
        foreach ($orderItemInfo as $item) {
            $orderHtml .="
                                <tr>
                                    <td height='25' align='center'><a href='http://www.akimimi.com/p/{$item["url_title"]}'>{$item["title"]}</a></td>
                                    <td align='center'>{$item["quantity"]}</td>
                                    <td align='center'>￥{$item["deal_price"]}</td>
                                </tr>
                          ";
        }
        
        $orderHtml .="                                
                                <tr>
                                    <td height='25' align='center'>&nbsp;</td>
                                    <td colspan='2' align='center'><h3 style='color:#47A618'>合计：￥{$orderTotal}</h3></td>
                                </tr>
                                ";
        
         $orderHtml .=
<<<str_result
                            </table>
                        </td>
                    </tr>
                    <tr style="color:#000000;line-height:22px;">
                        <td>
                            <span style="font-weight:bold; color:#FF0000;">温馨提示：</span>
                            您的订单已经确认，我们将在24小时内为您安排发货。阿基米米商城所<br/>
                            有商品均由合作品牌供应商提供，100%原厂正品。在此订单状态下，您不可以自行修<br/>
                            改或删除订单。如您希望修改或删除订单，请致电400-66-19686，由米米客服为您<br/>
                            效劳。<br/>                     
                            欢迎您再次到阿基米米购物，祝您购物愉快！
                            <br/>
                        </td>
                    </tr>
                </table>
            </td>
            <td style="width:175px;" valign="top">
                <img src="http://i.akimimi.com/t/default_zh_cn/image/email/email_007.jpg"  alt=""/>
            </td>
        </tr>
        <!-- center end -->
        
        <!-- center begin -->
        <tr>
            <td colspan="2" valign="top">
                <img src="http://i.akimimi.com/t/default_zh_cn/image/email/email_001.jpg" alt="" />
            </td>
        </tr>
        <!-- center end -->
    
</table>  
      
str_result;
        
        if(!empty($status)){
	        MailUtil::sendMail($email, $orderConfirmEmailTitle, $orderHtml);
	        //保存email日志
	        $email_type = 2;
	        $send_to = "$userName<$email>";
	        $send_cc = "";
	        $description = "订单<{$orderNumber}>已确认通知邮件";
	        $arr_param = array(
	            'email_type' => $email_type,
	            'send_to' => $send_to,
	            'send_cc' => $send_cc,
	            'description' => $description
	        );
	        $this->emailLogDao->insertData($arr_param);
        }
    }
    
    
    
    
    
    public function addEmailLog($emailInfo){
    	$this->emailLogDao->insertData($emailInfo);
    }
    
    
    
    
}